package 没有上司的舞会;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 6010, idx;
    static int[] w = new int[N];
    static int[] h = new int[N];
    static int[] e = new int[N];
    static int[] ne = new int[N];
    static int[][] f = new int[N][2];
    static boolean[] st = new boolean[N];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i++) {
            w[i] = in.nextInt();
        }
        for (int i = 1; i <= n - 1; i++) {
            int b = in.nextInt();
            int a = in.nextInt();
            add(a, b);
            st[b] = true;
        }
        int root = -1;
        for (int i = 1; i <= n; i++) {
            if (!st[i]) {
                root = i;
                break;
            }
        }
        dfs(root);
        System.out.println(Math.max(f[root][0], f[root][1]));
    }

    private static void dfs(int u) {
        f[u][1] = w[u];
        for (int i = h[u]; i != -1; i = ne[i]) {
            int j = e[i];
            dfs(j);
            f[u][1] += f[j][0];
            f[u][0] += Math.max(f[j][0], f[j][1]);
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
